package level.editor.view.group;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.util.ArrayList;

import javax.swing.Icon;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JSplitPane;

import level.editor.view.Window;
import level.editor.view.base.BasePanel;
import level.editor.view.desktop.Desktop;
import level.editor.view.frame.InternalFrame;

public class Group extends JPanel
{
	public Group()
	{
		super(new BorderLayout());
		init();
	}

	public ArrayList<Item> getItems()
	{
		ArrayList<Item> items = new ArrayList<Item>();

		if (isPanel())
		{
			SinglePanel panel = getPanel();
			BorderLayout layout = (BorderLayout) panel.getLayout();
			Item item = (Item) layout.getLayoutComponent(BorderLayout.CENTER);
			if (item != null)
				items.add(item);

		}

		if (isTabbed())
		{
			TabbedPane tabbed = getTabbed();
			for (int i = 0; i < tabbed.getComponentCount() - 1; i++)
			{
				Item item = (Item) tabbed.getComponentAt(i);
				if (item != null)
					items.add(item);
			}
		}
		if (items.size() > 0)
			return items;
		else
			return null;
	}

	public void add(Item item)
	{
		if (!isPanel() && !isTabbed())
		{
			SinglePanel panel = new SinglePanel(new BorderLayout());
			panel.add(item, BorderLayout.CENTER);
			add(panel, BorderLayout.CENTER);
		}
		else if (isPanel() && !isTabbed())
		{
			BorderLayout layout = (BorderLayout) getPanel().getLayout();
			Item itemOld = (Item) layout
					.getLayoutComponent(BorderLayout.CENTER);
			TabbedPane tabbed = new TabbedPane();
			tabbed.add(itemOld);
			tabbed.setTabComponentAt(tabbed.getTabCount() - 1,
					new TabComponent(tabbed));
			tabbed.add(item);
			tabbed.setTabComponentAt(tabbed.getTabCount() - 1,
					new TabComponent(tabbed));
			removeAll();
			add(tabbed, BorderLayout.CENTER);
			revalidate();
		}
		else if (!isPanel() && isTabbed())
		{
			TabbedPane tabbed = getTabbed();
			tabbed.add(item);
			tabbed.setTabComponentAt(tabbed.getTabCount() - 1,
					new TabComponent(tabbed));
			revalidate();
		}
	}

	public void add(Group group)
	{
		if (group.getItems() != null)
			for (Item item : group.getItems())
			{
				group.remove(item);
				add(item);
			}
	}

	public String getName()
	{
		String name = null;
		if (isPanel())
		{
			BorderLayout layout = (BorderLayout) getPanel().getLayout();
			Item item = (Item) layout.getLayoutComponent(BorderLayout.CENTER);
			if (item != null)
				name = item.getName();
		}
		else if (isTabbed())
		{
			if (getTabbed() != null
					&& getTabbed().getSelectedComponent() != null)
				name = getTabbed().getSelectedComponent().getName();
		}
		return name;
	}

	public Icon getIcon()
	{
		Icon icon = null;
		if (isPanel())
		{
			BorderLayout layout = (BorderLayout) getPanel().getLayout();
			Item item = (Item) layout.getLayoutComponent(BorderLayout.CENTER);
			if (item != null)
			{
				icon = item.getIcon();
			}
		}
		else if (isTabbed())
		{
			if (getTabbed() != null
					&& getTabbed().getSelectedComponent() != null)
				icon = ((Item) getTabbed().getSelectedComponent()).getIcon();
		}
		return icon;
	}

	public void removeCheck()
	{
		if (!isPanel() && isTabbed() && getTabbed().getTabCount() == 1
				&& getParent() instanceof JLayeredPane)
		{
			TabbedPane tabbed = getTabbed();
			SinglePanel panel = new SinglePanel(new BorderLayout());
			panel.add(tabbed.getComponentAt(0), BorderLayout.CENTER);
			removeAll();
			add(panel, BorderLayout.CENTER);
			revalidate();
		}
		else if (!isPanel() && isTabbed() && getTabbed().getTabCount() == 0
				&& !(getParent() instanceof JLayeredPane))
		{
			if (getParent() instanceof JSplitPane)
			{
				JSplitPane split = (JSplitPane) getParent();
				if (split.getParent() instanceof BasePanel)
				{
					BasePanel base = (BasePanel) split.getParent();
					base.removePanel(this);
				}
				else if (split.getParent() instanceof JSplitPane)
				{
					JSplitPane splitNew = (JSplitPane) split.getParent();
					if (splitNew.getParent() instanceof BasePanel)
					{
						BasePanel base = (BasePanel) splitNew.getParent();
						base.removePanel(this);
					}
				}
			}
		}
		else if (isPanel() && !(getParent() instanceof JLayeredPane))
		{
			BorderLayout layout = (BorderLayout) getPanel().getLayout();
			Item itemOld = (Item) layout
					.getLayoutComponent(BorderLayout.CENTER);
			TabbedPane tabbed = new TabbedPane();
			tabbed.add(itemOld);
			tabbed.setTabComponentAt(tabbed.getTabCount() - 1,
					new TabComponent(tabbed));
			removeAll();
			add(tabbed, BorderLayout.CENTER);
			revalidate();
		}
		Runtime.getRuntime().gc();
	}

	public Rectangle getGroupBound()
	{
		Rectangle rect = new Rectangle(getBounds());

		if (getRootPane() instanceof JRootPane)
		{
			rect.setLocation(rect.getLocation().x
					+ getRootPane().getLocation().x, rect.getLocation().y
					+ getRootPane().getLocation().y);
			if (getRootPane().getParent() instanceof InternalFrame)
			{
				InternalFrame frame = (InternalFrame) getRootPane().getParent();
				rect.setLocation(rect.getLocation().x + frame.getLocation().x,
						rect.getLocation().y + frame.getLocation().y);
			}
		}
		return rect;
	}

	public InternalFrame getFrame()
	{
		if (getRootPane() instanceof JRootPane)
			if (getRootPane().getParent() instanceof InternalFrame)
			{
				InternalFrame frame = (InternalFrame) getRootPane().getParent();
				return frame;
			}
		return null;
	}

	public Window getWindow()
	{
		if (getDesktop() != null)
			return (Window) getDesktop().getTopLevelAncestor();

		else
			return (Window) getTopLevelAncestor();

	}

	@Override
	public Dimension getMinimumSize()
	{
		Dimension newDim = new Dimension(0, 0);
		if (getItems() != null)
		{
			for (Item item : getItems())
			{
				newDim.setSize((int) (newDim.getWidth() > item.getMinimumSize()
						.getWidth() ? newDim.getWidth() : item.getMinimumSize()
						.getWidth()), (int) (newDim.getHeight() > item
						.getMinimumSize().getHeight() ? newDim.getHeight()
						: item.getMinimumSize().getHeight()));
			}
		}
		return newDim;
	}

	private Desktop getDesktop()
	{
		if (getRootPane() instanceof JRootPane)
			if (getRootPane().getParent() instanceof InternalFrame)
			{
				InternalFrame frame = (InternalFrame) getRootPane().getParent();
				if (frame.getDesktopPane() instanceof Desktop)
				{
					return (Desktop) frame.getDesktopPane();
				}
			}
		return null;
	}

	private SinglePanel getPanel()
	{
		if (isPanel())
			return (SinglePanel) ((BorderLayout) getLayout())
					.getLayoutComponent(BorderLayout.CENTER);
		else
			return null;
	}

	private Boolean isPanel()
	{
		return ((((BorderLayout) getLayout())
				.getLayoutComponent(BorderLayout.CENTER) instanceof SinglePanel) ? true
				: false);
	}

	private TabbedPane getTabbed()
	{
		if (isTabbed())
			return (TabbedPane) ((BorderLayout) getLayout())
					.getLayoutComponent(BorderLayout.CENTER);
		else
			return null;
	}

	private Boolean isTabbed()
	{
		return ((((BorderLayout) getLayout())
				.getLayoutComponent(BorderLayout.CENTER) instanceof TabbedPane) ? true
				: false);
	}

	private void init()
	{
		setBorder(null);
	}

}
